﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using Newtonsoft.Json;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_PollingResultController : MvcControllerBase
    {
        private PollingBLL bll = new PollingBLL();
        private PollingResultBLL prbll = new PollingResultBLL();

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Detail()
        {
            return View();
        }

        /// <summary>
        /// 查询巡检项目
        /// </summary>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public ActionResult GetItemTreeJson(string keyWord)
        {
            var itemDetail = new DataItemDetailBLL().GetList("PollingType").FirstOrDefault(t => t.ItemCode == keyWord);
            if (itemDetail == null) { throw new Exception("该巡检专业不存在！"); }
            var itemDetailId = itemDetail.ItemDetailId;
            var data = new PollingBLL().GetItemList().Where(t => t.Type == itemDetailId).ToList();
            var treeList = new List<TreeEntity>();
            treeList.Add(new TreeEntity() { id = "All", value = "All", text = "所有巡检项目", parentId = "0", hasChildren = true, isexpand = true, complete = true });
            foreach (PollingItemEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = string.IsNullOrEmpty(item.ParentId) ? "All" : item.ParentId;
                tree.img = null;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }

        public ActionResult GetResultListJson(Pagination pagination, string queryJson)
        {
            try
            {

                var queryParam = queryJson.ToJObject();
                int Category = Convert.ToInt32(queryParam["Category"]);
                DateTime EndTime = Convert.ToDateTime(queryParam["EndTime"]);
                DateTime StartTime = Convert.ToDateTime(queryParam["StartTime"]);
                string keyWord = queryParam["keyWord"].ToString();
                string ItemId = queryParam["ItemId"].ToString();
                string OrganizeId = queryParam["OrganizeId"].ToString();
                var itemDetail = new DataItemDetailBLL().GetList("PollingType").FirstOrDefault(t => t.ItemCode == keyWord);
                if (itemDetail == null) { throw new Exception("该巡检专业不存在！"); }
                var itemDetailId = itemDetail.ItemDetailId;
                var datalist = !string.IsNullOrEmpty(OrganizeId) ? new PollingResultBLL().GetList(itemDetailId, StartTime, EndTime, ItemId, "").Where(t => t.OrganizeId == OrganizeId).GroupBy(t => t.ObjectId).ToList() : new PollingResultBLL().GetList(itemDetailId, StartTime, EndTime, ItemId).GroupBy(t => t.ObjectId).ToList();

                IList<object> ColMs = new List<object>();
                int intervalDate = 0, rows = 0, sum = 0, count = 0;
                string str = "[";
                switch (Category)
                {
                    case 1:
                        {
                            #region //日看板
                            intervalDate = (EndTime - StartTime).Days;
                            if (datalist != null && datalist.Count() > 0)
                            {
                                foreach (var item in datalist)
                                {
                                    if (rows != 0) str += ",";
                                    if (keyWord == "SP_XJ")
                                    {
                                        RelationPCEntity pc = new RelationPCBLL().GetEntity(item.Key, "PStructId");
                                        if (pc == null) { continue; }
                                        PStructEntity ps = new PStructBLL().GetEntity(pc.PStructId);
                                        if (ps == null) { continue; }
                                        CustomerInfoEntity cs = new CustomerInfoBLL().GetEntity(pc.CustomerId);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + ps.Name + (cs != null ? "(" + cs.Name + ")\"" : "(" + ")\"") + ",\"Code\":\"" + ps.Code + "\"";
                                    }
                                    else if (keyWord == "SB_XJ")
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        DeviceEntity device = new DeviceBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : (device != null ? device.Name : "")) + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : (device != null ? device.Code : "")) + "\"";
                                    }
                                    else
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : "") + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : "") + "\"";
                                    }
                                    for (int i = 0; i <= intervalDate; i++)
                                    {
                                        DateTime date = StartTime.AddDays(i);
                                        if (rows == 0)
                                        {
                                            object colms = new object();
                                            if (i == 0)
                                            {
                                                ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                                ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                                if (keyWord == "SP_XJ")
                                                {
                                                    ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                else
                                                {
                                                    ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                            }
                                            ColMs.Add(new { label = date.ToString("yyyy-MM-dd") + "<br/>(" + Time.GetWeekNameOfDay(date) + ")", name = "Date" + i, width = 100, align = "center", sortable = false });
                                            if (i == intervalDate) ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                        }
                                        count = item.Count(t => t.Date.Value.ToString("yyyy-MM-dd") == date.ToString("yyyy-MM-dd"));
                                        sum += count;
                                        str += ",\"Date" + i + "\":" + count;
                                    }
                                    str += ",\"Sum\":" + sum + "}";
                                    sum = 0;
                                    rows++;
                                }
                            }
                            else
                            {
                                for (int i = 0; i <= intervalDate; i++)
                                {
                                    DateTime date = StartTime.AddDays(i);
                                    if (i == 0)
                                    {
                                        ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                        ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                        if (keyWord == "SP_XJ")
                                        {
                                            ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        else
                                        {
                                            ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                    }
                                    ColMs.Add(new { label = date.ToString("yyyy-MM-dd") + "<br/>(" + Time.GetWeekNameOfDay(date) + ")", name = "Date" + i, width = 100, align = "center", sortable = false });
                                    if (i == intervalDate)
                                    {
                                        ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                    }
                                }
                            }
                            #endregion
                        } break;
                    case 2:
                        {
                            #region //月看板
                            intervalDate = (EndTime.Month - StartTime.Month) + ((EndTime.Year - StartTime.Year) * 12);
                            if (datalist != null && datalist.Count() > 0)
                            {
                                foreach (var item in datalist)
                                {
                                    if (rows != 0) str += ",";
                                    if (keyWord == "SP_XJ")
                                    {
                                        RelationPCEntity pc = new RelationPCBLL().GetEntity(item.Key, "PStructId");
                                        if (pc == null) { continue; }
                                        PStructEntity ps = new PStructBLL().GetEntity(pc.PStructId);
                                        if (ps == null) { continue; }
                                        CustomerInfoEntity cs = new CustomerInfoBLL().GetEntity(pc.CustomerId);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + ps.Name + (cs != null ? "(" + cs.Name + ")\"" : "(" + ")\"") + ",\"Code\":\"" + ps.Code + "\"";
                                    }
                                    else if (keyWord == "SB_XJ")
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        DeviceEntity device = new DeviceBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : (device != null ? device.Name : "")) + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : (device != null ? device.Code : "")) + "\"";
                                    }
                                    else
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : "") + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : "") + "\"";
                                    }
                                    for (int i = 0; i <= intervalDate; i++)
                                    {
                                        DateTime date = StartTime.AddMonths(i);
                                        if (rows == 0)
                                        {
                                            object colms = new object();
                                            if (i == 0)
                                            {
                                                ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                                ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                                if (keyWord == "SP_XJ")
                                                {
                                                    ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                else
                                                {
                                                    ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                            }
                                            ColMs.Add(new { label = date.Year + "年" + date.Month + "月", name = "Date" + i, width = 90, align = "center", sortable = false });
                                            if (i == intervalDate) ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                        }
                                        count = item.Count(t => t.Date.Value.Year == date.Year && t.Date.Value.Month == date.Month);
                                        sum += count;
                                        str += ",\"Date" + i + "\":" + count;
                                    }
                                    str += ",\"Sum\":" + sum + "}";
                                    sum = 0;
                                    rows++;
                                }
                            }
                            else
                            {
                                for (int i = 0; i <= intervalDate; i++)
                                {
                                    DateTime date = StartTime.AddMonths(i);
                                    if (i == 0)
                                    {
                                        ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                        ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                        if (keyWord == "SP_XJ")
                                        {
                                            ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        else
                                        {
                                            ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                    }
                                    ColMs.Add(new { label = date.Year + "年" + date.Month + "月", name = "Date" + i, width = 90, align = "center", sortable = false });
                                    if (i == intervalDate)
                                    {
                                        ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                    }
                                }
                            }
                            #endregion
                        }; break;
                    case 3:
                        {
                            #region //年看板
                            intervalDate = (EndTime.Year - StartTime.Year);
                            if (datalist != null && datalist.Count() > 0)
                            {
                                foreach (var item in datalist)
                                {
                                    if (rows != 0) str += ",";
                                    if (keyWord == "SP_XJ")
                                    {
                                        RelationPCEntity pc = new RelationPCBLL().GetEntity(item.Key, "PStructId");
                                        if (pc == null) { continue; }
                                        PStructEntity ps = new PStructBLL().GetEntity(pc.PStructId);
                                        if (ps == null) { continue; }
                                        CustomerInfoEntity cs = new CustomerInfoBLL().GetEntity(pc.CustomerId);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + ps.Name + (cs != null ? "(" + cs.Name + ")\"" : "(" + ")\"") + ",\"Code\":\"" + ps.Code + "\"";
                                    }
                                    else if (keyWord == "SB_XJ")
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        DeviceEntity device = new DeviceBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : (device != null ? device.Name : "")) + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : (device != null ? device.Code : "")) + "\"";
                                    }
                                    else
                                    {
                                        PollingAreaEntity area = new PollingBLL().GetEntity(item.Key);
                                        str += "{\"Id\":\"" + item.Key + "\",\"Type\":\"" + itemDetailId + "\",\"Name\":\"" + (area != null ? area.Name : "") + "\"" + ",\"Code\":\"" + (area != null ? area.EnCode : "") + "\"";
                                    }

                                    for (int i = 0; i <= intervalDate; i++)
                                    {
                                        DateTime date = StartTime.AddYears(i);
                                        if (rows == 0)
                                        {
                                            object colms = new object();
                                            if (i == 0)
                                            {
                                                ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                                ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                                if (keyWord == "SP_XJ")
                                                {
                                                    ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                else
                                                {
                                                    ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                                }
                                                ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                            }
                                            ColMs.Add(new { label = date.Year + "年", name = "Date" + i, width = 90, align = "center", sortable = false });
                                            if (i == intervalDate) ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                        }
                                        count = item.Count(t => t.Date.Value.Year == date.Year);
                                        sum += count;
                                        str += ",\"Date" + i + "\":" + count;
                                    }
                                    str += ",\"Sum\":" + sum + "}";
                                    sum = 0;
                                    rows++;
                                }
                            }
                            else
                            {
                                for (int i = 0; i <= intervalDate; i++)
                                {
                                    DateTime date = StartTime.AddYears(i);
                                    if (i == 0)
                                    {
                                        ColMs.Add(new { label = "主键", name = "Id", hidden = true });
                                        ColMs.Add(new { label = "专业", name = "Type", hidden = true });
                                        if (keyWord == "SP_XJ")
                                        {
                                            ColMs.Add(new { label = "商铺", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        else
                                        {
                                            ColMs.Add(new { label = "巡检点", name = "Name", width = 90, align = "center", sortable = false });
                                        }
                                        ColMs.Add(new { label = "编号", name = "Code", width = 100, align = "center", sortable = false });
                                    }
                                    ColMs.Add(new { label = date.Year + "年", name = "Date" + i, width = 90, align = "center", sortable = false });
                                    if (i == intervalDate)
                                    {
                                        ColMs.Add(new { label = "小计", name = "Sum", width = 90, align = "center", sortable = false });
                                    }
                                }
                            }
                            #endregion
                        }; break;

                }

                str += "]";
                object obj = JsonConvert.DeserializeObject<object>(str);
                var coldata = new { total = rows, page = pagination.page, records = rows, rows = obj };
                var data = new { ColMs = ColMs, Coldata = coldata };
                return Content(data.ToJson());
            }
            catch (Exception e)
            {
                throw;
            }
        }

        public ActionResult GetResultDataList(string Bcode, DateTime BeginTime, DateTime EndTime, string Icode = "", string ObjectId = "")
        {
            var list = new PollingResultBLL().GetList(Bcode, BeginTime, EndTime, Icode, ObjectId).Join(new PollingResultBLL().GetDetailList(), x => x.Id, y => y.PollingResultId, (x, y) => new
            {
                x.Date,
                y.IsException,
                DetailId = y.Id,
                x.Id,
                x.Icode
            }).Join(bll.GetItemList("", "", Bcode), x => x.Icode, y => y.Id, (x, y) => new
            {
                x.Date,
                x.Icode,
                x.Id,
                x.IsException,
                x.DetailId,
                ItemName = y.Name
            }).GroupBy(x => new { x.Date, x.Icode });
            List<object> data = new List<object>();
            foreach (var item in list)
            {
                var model = item.First();
                data.Add(new { Count = item.Count(), IsException = item.Count(t => t.IsException == 0), Date = model.Date, Id = model.Id, ItemName = model.ItemName });
            }
            return Content(data.ToJson());
        }

        public ActionResult GetResultList(Pagination pagination, string queryJson)
        {
            var queryParam = queryJson.ToJObject();
            int Category = Convert.ToInt32(queryParam["Category"]);
            DateTime StartTime = Convert.ToDateTime(queryParam["StartTime"]);
            int DateAdd = Convert.ToInt32(queryParam["DateAdd"]);
            DateTime EndTime = new DateTime();
            switch (Category)
            {
                case 1: StartTime = StartTime.AddDays(DateAdd - 1); EndTime = StartTime.AddDays(1).AddSeconds(-1); break;
                case 2: StartTime = StartTime.AddMonths(DateAdd - 1); EndTime = StartTime.AddMonths(1).AddSeconds(-1); break;
                case 3: StartTime = StartTime.AddYears(DateAdd - 1); EndTime = StartTime.AddYears(1).AddSeconds(-1); break;
            }
            var ResultData = new PollingResultBLL().GetList(pagination, queryParam["TypeId"].ToString(), StartTime, EndTime, queryParam["ItemId"].ToString(), queryParam["ObjectId"].ToString());
            var datalist = from a in ResultData
                           select new
                           {
                               a.Id,
                               a.Icode,
                               a.Bcode,
                               a.Bname,
                               Iname = new PollingBLL().GetItemEntity(a.Icode).Name,
                               a.Date,
                               a.CreateUserName
                           };

            var watch = CommonHelper.TimerStart();
            var JsonData = new
            {
                rows = datalist,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records,
                costtime = CommonHelper.TimerEnd(watch)
            };
            return Content(JsonData.ToJson());
        }

        public ActionResult GetResultDetail(string ResultId, string itemId)
        {
            if (string.IsNullOrEmpty(ResultId))
            {
                var dataContent = new PollingBLL().GetContentList().Select(t => new { t.Type, t.Name, t.TypeDescription });
                return Content(dataContent.ToJson());
            }
            else
            {
                var dataDetail = prbll.GetDetailList(ResultId);
                var dataContent = bll.GetContentList();
                var data = dataDetail.Join(dataContent, x => x.ContentId, y => y.Id, (x, y) => new { y.Type, y.Name, x.Text, x.Value, x.IsException, x.Memo, y.TypeDescription, y.Criterion, y.CheckWay });
                StringBuilder _Html = new StringBuilder();
                int index = 1;
                if (data != null && data.Count() > 0)
                {
                    foreach (var item in data)
                    {
                        _Html.Append("<tr style=\"border-bottom:1px dashed #CCC;\"><td>");
                        _Html.Append("<p><strong>" + index + "." + item.Name + "</strong></p><p style=\"padding-left:15px;\">标准要求：" + item.Criterion + "</p>");
                        _Html.Append("<p style=\"padding-left:15px;\">检查方法：" + item.CheckWay + "</p>");
                        _Html.Append("<p style=\"padding-left:15px;\">巡检结果：" + item.Text + "," + item.Memo + "</p><p>");
                        _Html.Append("<p style=\"padding-left:15px;\">是否异常：" + (item.IsException == 1 ? "异常" : "无异常") + "</p><p>");
                        _Html.Append("</p></td></tr>");
                        index++;
                    }
                }
                StringBuilder FileHtml = new StringBuilder();
                var dataFile = new AttachFileBLL().GetList(ResultId);
                if (dataFile != null && dataFile.Count() > 0)
                {
                    foreach (var item in dataFile)
                    {
                        if (item.FileType == 1 && !string.IsNullOrEmpty(item.FileUrl))
                        {
                            var url = item.FileUrl.Replace("\\", "/");
                            FileHtml.Append("<img src=\"" + url + "\"  style='float: left; width: 100px; height: 100px; position: relative;padding: 10px;' />");
                        }
                        else if (item.FileType == 3 && !string.IsNullOrEmpty(item.FileUrl))
                        {
                            FileHtml.Append("<video controls><source src=\"~/Content/images/video/1481808535517.mp4\" type=\"video/mp4\" /></video>");
                        }
                    }
                }

                object obj = new { Html = _Html.ToString(), FileHtml = FileHtml.ToString() };
                return Content(obj.ToJson());
            }
        }



        /// <summary>
        /// 查询巡检项目情况
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        /// --目前仅用于装修卡片
        public ActionResult GetPollingItemInfo(string queryJson)
        {
            try
            {
                if (string.IsNullOrEmpty(queryJson)) throw new Exception("请传入巡检参数不能为空");
                var queryParam = queryJson.ToJObject();
                if (queryParam["Type"].IsEmpty()) throw new Exception("请传入巡检种类不能为空");
                string Type = queryParam["Type"].ToString();
                string ObjectId = queryParam["ObjectId"].ToString();
                var dataItem = new PollingBLL().GetItemList("", "", Type);
                var datalist = new PollingResultBLL().GetCountList(queryJson);
                if (datalist != null && datalist.Rows.Count > 0)
                {
                    var data = from x in dataItem
                               join y in datalist.AsEnumerable()
                               on x.Id equals y.Field<string>("Icode") into temp
                               from tt in temp.DefaultIfEmpty()
                               select new
                               {
                                   x.Id,
                                   x.Code,
                                   x.Name,
                                   x.TypeName,
                                   x.UnitNum,
                                   x.Unit,
                                   x.Frenquency,
                                   x.Memo,
                                   Count = tt == null ? 0 : tt.Field<int>("count"),
                                   ObjectId = tt == null ? "" : tt.Field<string>("objectid"),
                                   Date = tt == null ? "" : tt.Field<string>("date")
                               };
                    return Content(data.ToJson());
                }
                else
                {
                    var data = from x in dataItem
                               select new
                               {
                                   x.Id,
                                   x.Code,
                                   x.Name,
                                   x.TypeName,
                                   x.UnitNum,
                                   x.Unit,
                                   x.Frenquency,
                                   x.Memo,
                                   Count = 0,
                                   ObjectId = ""
                               };
                    return Content(data.ToJson());
                }
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }

        }

        /// <summary>
        /// 查询设备巡检项目情况
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        public ActionResult GetPollingDeviceItemInfo(string queryJson)
        {
            try
            {

                if (!string.IsNullOrEmpty(queryJson))
                {
                    var queryParam = queryJson.ToJObject();
                    if (!queryParam["Type"].IsEmpty())
                    {
                        string Type = queryParam["Type"].ToString();
                        string ObjectId = queryParam["ObjectId"].ToString();
                        if (string.IsNullOrEmpty(ObjectId))
                            throw new Exception("请传入巡检设备不能为空");
                        var device = new DeviceBLL().GetEntity(ObjectId);
                        if (device == null)
                            throw new Exception("该巡检设备不存在");
                        var organize = new OrganizeBLL().GetEntity(device.OrganizeId);
                        if (organize == null)
                            throw new Exception("该巡检设备不存在组织机构，请更改设备资料");
                        var obj = new { DeviceTypeId = device.Type, DeviceLevel = device.Level, ProjectType = organize.BusinessScope }.ToJson();
                        var RelationDeviceItem = new PollingBLL().GetRelationDeviceItemList(1, obj);
                        var dataItem = new PollingBLL().GetItemList("", "", Type).Join(RelationDeviceItem, x => x.Id, y => y.ItemId, (x, y) => new
                        {
                            x.Id,
                            x.Code,
                            x.Name,
                            x.TypeName,
                            x.UnitNum,
                            x.Unit,
                            x.Frenquency,
                            x.Memo,
                        });
                        var datalist = new PollingResultBLL().GetCountList(queryJson);

                        if (datalist != null && datalist.Rows.Count > 0)
                        {
                            var data = from x in dataItem
                                       join y in datalist.AsEnumerable()
                                       on x.Id equals y.Field<string>("Icode") into temp
                                       from tt in temp.DefaultIfEmpty()
                                       select new
                                       {
                                           x.Id,
                                           x.Code,
                                           x.Name,
                                           x.TypeName,
                                           x.UnitNum,
                                           x.Unit,
                                           x.Frenquency,
                                           x.Memo,
                                           Count = tt == null ? 0 : tt.Field<int>("count"),
                                           ObjectId = tt == null ? "" : tt.Field<string>("objectid"),
                                           Date = tt == null ? "" : tt.Field<string>("date")
                                       };
                            return Content(data.ToJson());
                        }
                        else
                        {
                            var data = from x in dataItem
                                       select new
                                       {
                                           x.Id,
                                           x.Code,
                                           x.Name,
                                           x.TypeName,
                                           x.UnitNum,
                                           x.Unit,
                                           x.Frenquency,
                                           x.Memo,
                                           Count = 0,
                                           ObjectId = ""
                                       };
                            return Content(data.ToJson());
                        }
                    }
                    else
                        throw new Exception("请传入巡检种类不能为空");
                }
                else
                    throw new Exception("请传入巡检参数不能为空");
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }
        }

        /// <summary>
        /// 查询巡检内容明细
        /// </summary>
        /// <param name="ObjectId"></param>
        /// <param name="ItemId"></param>
        /// <returns></returns>
        public ActionResult GetPollingContentInfo(string ObjectId, string ItemId)
        {
            var data = new PollingResultBLL().GetContentList(ObjectId, ItemId);
            return Content(data.ToJson());
        }
    }
}
